module.exports = class extends think.Controller {
  constructor(...arg) {
    super(...arg);
  }
  indexAction(){
      return this.display();
  }

  openAction(){
    this.emit("open","This Client opened successfully !");
    this.broadcast("joined","There is a new client joined successfully !");
  }

  socketAction(){
    return this.display("socket.io.html");
  }

  async queryUserAction() {
    try{
      console.log("获取客户端 queryUser 事件发送的数据", this.wsData);
      //console.log("获取当前 websocket 对象", this.websocket);
      console.log("判断当前请求是否是 websocket 请求", this.isWebsocket);
      const user=await this.model("user").where({id:this.wsData}).select();
      if (user.length!==0){
        this.broadcast("push", user);
      }else {
        this.broadcast("push", [{id:"none",name:"none",age:"none"}]);
      }
    }catch (e){
      this.fail(10000,e.toString());
    }
  }

  closeAction(){
    console.log("socket.io Server was closed");
  }

};
